package com.icss.ah.vehicleInfo.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.icss.ah.base.action.BaseAction;
import com.icss.ah.base.bean.EasyuiDataGridJson;
import com.icss.ah.base.bean.Json;
import com.icss.ah.base.util.MD5Util;
import com.icss.ah.login.bean.UserInfoBean;
/**
 * 车辆信息维护Action
 * @author wangjun
 *
 */
@Controller
@Scope("prototype")
public class VehicleInfoAction extends BaseAction {
	private final Logger errlog = Logger.getLogger("E");
	private final Logger infolog = Logger.getLogger("I");
	private String operat;			//操作标识
	private String carUuid;			//车辆uuid
	private String carName;			//车辆名称
	private String carNo;			//车牌号码
	private String carColor;		//车辆颜色
	private String sortNum;			//排序
	/**
	 * 进入车辆列表界面
	 * @return
	 */
	public String toVehicleList(){
		request = this.getRequest();
		return "toVehicleList";
	}
	/**
	 * 查询车辆列表
	 */
	public void queryVehicleList(){
		request = this.getRequest();
		String result = "";
		try {
			UserInfoBean userInfo=getUserInfo("");
			String shopId=userInfo.getShopId();
			String corpId = userInfo.getCorpId();
			
			String carNo = request.getParameter("carNo");//车牌号（查询条件）
			carNo = carNo == null ? "" : carNo;
			dataGridJson = new EasyuiDataGridJson();
			HashMap map = new HashMap();
			map.put("corpId",corpId);
			map.put("shopId",shopId);
			map.put("carNo",carNo);
			map.put("startNo",Integer.valueOf(getStartIndexMysql()));
			map.put("pageRows",Integer.valueOf(getEndIndexMysql()));
			List<Map> vehicleList = new ArrayList();
			vehicleList = vehicleInfoService.queryVehicleList(map);	
			long vehicleCount = vehicleInfoService.queryVehicleCount(map);//查询总记录数
			dataGridJson.setRows(vehicleList);
			dataGridJson.setTotal(vehicleCount); 
			result = JSONObject.fromObject(dataGridJson).toString();
			infolog.info("queryVehicleList|查询车辆信息列表成功");
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("queryVehicleList|查询车辆信息列表失败：",e);
		} finally {
			str2Resp(result);
		}
	}
	
	/**
	 * 进入编辑车辆信息界面
	 * @return
	 */
	public String editVehicle(){
		request = this.getRequest();
		String carUuid = request.getParameter("carUuid");
		carUuid = carUuid == null ? "" : carUuid;
		HashMap map = new HashMap();
		map.put("operat", operat);
		Map vehicleMap = new HashMap();
		if("edit".equals(operat)){
			map.put("carUuid", carUuid);
			vehicleMap = vehicleInfoService.queryVehicleMap(map);
		}
		request.setAttribute("vehicleMap", vehicleMap);
		request.setAttribute("operat", operat);
		return "toVehicleEdit";
	}
	
	/**
	 * 保存车辆信息操作(添加或修改)
	 * @return
	 */
	public String saveVehicle() {
		request = this.getRequest();
		jsonText = new Json();
		try {
			UserInfoBean userInfo=getUserInfo("");
			String shopId=userInfo.getShopId();
			String corpId = userInfo.getCorpId();
			
			HashMap map = new HashMap();
			map.put("carUuid",carUuid );
			map.put("carName", carName);
			map.put("carNo",carNo );
			map.put("carColor", carColor);
			map.put("sortNum",sortNum );
			map.put("corpId", corpId);
			map.put("shopId", shopId);
			boolean revl = false;
			revl =  vehicleInfoService.saveVehicle(map,carUuid,operat);
			if(revl){
				jsonText.setMsg("保存成功！");
				jsonText.setSuccess(true);
			}else{
				jsonText.setMsg("保存失败！");
				jsonText.setSuccess(true);
			}
		} catch (RuntimeException e) {
			e.printStackTrace();
		}
		return "saveVehicle";
	}
	
	/**
	 * 删除车辆
	 * @return
	 */
	public String deleteVehicle(){
		request = this.getRequest();
		jsonText = new Json();
		try {
			String carUuid = request.getParameter("carUuid");
			carUuid = carUuid == null ? "" : carUuid;
			HashMap map = new HashMap();
			map.put("carUuid", carUuid);
			boolean revl = false;
			revl = vehicleInfoService.deleteVehicle(map);
			jsonText.setSuccess(true);
			jsonText.setMsg("删除成功！");
		} catch (Exception e) {
			jsonText.setMsg("删除失败！");
			jsonText.setSuccess(false);
		}
		return "jsonText";
	}
	/**
	 * 停用启用操作
	 * @return
	 */
	public String dealStatus(){
		request = this.getRequest();
		jsonText = new Json();
		try {
			String carUuid = request.getParameter("carUuid");//车辆id
			carUuid = carUuid == null ? "" : carUuid;
			String status = request.getParameter("status");//停用或启用状态
			status = status == null ? "" : status;
			HashMap map = new HashMap();
			map.put("carUuid", carUuid);
			map.put("status", status);
			boolean revl = false;
			revl = vehicleInfoService.dealStatus(map);
			jsonText.setSuccess(true);
			jsonText.setMsg("操作成功！");
		} catch (Exception e) {
			jsonText.setMsg("操作失败！");
			jsonText.setSuccess(false);
		}
		return "jsonText";
	}
	/**
	 * 验证车牌号是否存在
	 * @return
	 */
	public String checkCarNoExist(){
		request = this.getRequest();
		jsonText = new Json();
		try {
			HashMap map = new HashMap();
			map.put("carNo", carNo);
			boolean revl = false;
			revl = vehicleInfoService.checkCarNoExist(map);
			jsonText.setSuccess(revl);
			if(revl){
				jsonText.setMsg("验证通过");
			} else {
				jsonText.setMsg("车牌号已存在");
			}
		} catch (Exception e) {
			jsonText.setMsg("验证失败");
			jsonText.setSuccess(false);
		}
		return "jsonText";
	}
	public String getOperat() {
		return operat;
	}
	public void setOperat(String operat) {
		this.operat = operat;
	}
	public String getCarUuid() {
		return carUuid;
	}
	public void setCarUuid(String carUuid) {
		this.carUuid = carUuid;
	}
	public String getCarName() {
		return carName;
	}
	public void setCarName(String carName) {
		this.carName = carName;
	}
	public String getCarNo() {
		return carNo;
	}
	public void setCarNo(String carNo) {
		this.carNo = carNo;
	}
	public String getCarColor() {
		return carColor;
	}
	public void setCarColor(String carColor) {
		this.carColor = carColor;
	}
	public String getSortNum() {
		return sortNum;
	}
	public void setSortNum(String sortNum) {
		this.sortNum = sortNum;
	}
	

}
